home *** CD-ROM | disk | FTP | other *** search
/ Java for 3D & VRML Worlds / Java for 3d and VRML Worlds.iso / examples / chap05 / ExampleClient.java < prev    next >
Encoding:
Java Source  |  1996-10-07  |  2.6 KB  |  86 lines

  1. // ExampleClient.java
  2. // Send a current position to server and then get a new position 
  3. // from the server
  4. import vrml.*;
  5. import vrml.field.*;
  6. import vrml.node.*;
  7.  
  8. import java.net.*;
  9. import java.io.*;
  10. import java.util.*;
  11.  
  12. public class ExampleClient extends Script{
  13.    public final static int PORT = 4130;
  14.    public final static String HOST = "localhost"; // server host name
  15.  
  16.    Socket socket = null;
  17.    DataInputStream in = null;     // input stream from server to client
  18.    DataOutputStream out = null;   // output sream from client to server
  19.    Browser b;                     // for displaying error message to user
  20.    Node target = null;
  21.    SFVec3f trans = null;         
  22.    float[] coord = null;
  23.    float[] rotation = null;
  24.  
  25.    public void initialize(){
  26.       b = getBrowser();
  27.       // get the reference to 'translation' field of 'target' node
  28.       target = (Node)((SFNode)getField("target")).getValue();
  29.       trans = (SFVec3f)target.getExposedField("translation");
  30.       coord = new float[3];
  31.       rotation = new float[4];
  32.  
  33.       try {
  34.          // open network and input/output stream
  35.          socket = new Socket("localhost", PORT);
  36.          in = new DataInputStream(socket.getInputStream());
  37.          out = new DataOutputStream(socket.getOutputStream());
  38.       } catch (UnknownHostException e) {
  39.          b.setDescription("Unknown host: " + HOST);
  40.       } catch (Exception e) {
  41.          b.setDescription("Connection error");
  42.       }
  43.    }
  44.  
  45.    public void processEvent(Event ev) {
  46.      ((ConstSFRotation)ev.getValue()).getValue(rotation); // get rotation angle
  47.      
  48.       try {
  49.          // get current position
  50.          trans.getValue(coord);
  51.  
  52.      // send collision angle
  53.      out.writeFloat(rotation[3]);
  54.  
  55.          // send current position to server
  56.          out.writeFloat(coord[0]);
  57.          out.writeFloat(coord[1]);
  58.          out.writeFloat(coord[2]);
  59.  
  60.          // receive new position from server
  61.          coord[0] = in.readFloat();
  62.          coord[1] = in.readFloat();
  63.          coord[2] = in.readFloat();
  64.  
  65.          // display new position 
  66.          b.setDescription("position: " + coord[0] + "," + 
  67.              coord[1] + "," + coord[2]);
  68.  
  69.          // set new position 
  70.          trans.setValue(coord);
  71.       } catch (IOException e) {
  72.          b.setDescription("IOException:  " + e);
  73.       }
  74.    }
  75.  
  76.    public void shutdown(){ // closing stream and network
  77.       try {
  78.          out.close();
  79.          in.close();
  80.          socket.close();
  81.       } catch (Exception e) {
  82.          b.setDescription("Connection close error");
  83.       }
  84.    }
  85. }
  86.